/***************************************************************************
 *   oboe.controllers.soundController.SoundController                      *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.soundController
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.utils.Dictionary;
	import flash.utils.getTimer;
	
	import oboe.core.Oboe;
	import oboe.core.SigSlot;
	import oboe.resources.Files;
	
	public class SoundController extends SigSlot
	{
		public static const instance:SigSlot = new SoundController();
		
		private var music:ManagedSound;
		private var sounds:Dictionary;
		private var soundClasses:Dictionary;
		
		public function SoundController()
		{
			super( );
			sounds = new Dictionary();
			soundClasses = new Dictionary();
			Oboe.flags["SOUNDMUTED"] = Oboe.flags["NO_SOUND"] === true;
		}
		
		public function slot_playMusic( name:String ):void
		{
			if( music ) music.stop();
			if( name != null )
			{
				music = new ManagedSound( Sound(Files.getFile( name )), onSoundEnd, name, "" );
			}
			music.play( 0, -1 );
		}
		
		public function slot_stopMusic():void
		{
			if(music)
			{
				music.stop();	
			}			
		}
		
		public function slot_stopSoundClass( soundClass:String ):void
		{
			if( !(soundClass in this.soundClasses) ) return;
			var scd:Dictionary = this.soundClasses[ soundClass ];
			for each( var s:ManagedSound in scd )
			{
				s.stopAll();
			}
		}

		public function slot_stopAllSounds():void
		{
			for( var soundClass:String in this.soundClasses )
			{
				this.slot_stopSoundClass( soundClass );
			}
		}
		
		public function slot_playSound( name:String, loops:int=1, soundClass:String="" ):void
		{
			var snd:ManagedSound;

			if( Oboe.flags["SOUND_OVERLAP"] === false )
			{
				this.slot_stopSoundClass( soundClass );
			}

			if( soundClass == "" ) soundClass = name;
			
			if( !(name in sounds) )
			{
				sounds[ name ] = new ManagedSound( Files.getFile(name) as Sound, onSoundEnd, name, soundClass );
			}
			
			snd = sounds[ name ];
			
			if( !(soundClass in this.soundClasses) )
			{
				this.soundClasses[ soundClass ] = new Dictionary( true );
			}
			this.soundClasses[ soundClass ][ snd ] = snd;
			
			
			
			snd.play( 0, loops);
		}
		
		private function onSoundEnd( sound:ManagedSound, channel:ManagedChannel ):void
		{
			if( sound.getChannelCount() == 1 )
			{
				delete sounds[ sound.getName() ];
				var sc:String = sound.getSoundClass();
				if( sc in soundClasses && sound in soundClasses[sc] ) // fix the symptom, fix the problem
				{
					delete soundClasses[ sound.getSoundClass() ][ sound ];
				}
			}
			else
			{
				sound.removeChannel( channel );
			}
		}
		
		public function slot_pauseSounds():void
		{
			if( this.music ) music.pause();
			for each( var snd:ManagedSound in this.sounds )
			{
				snd.pause();
			}
			Oboe.flags["SOUNDMUTED"] = true;
		}
		
		public function slot_continueSounds():void
		{
			if( this.music ) music.resumeAfter( 0 );
			
			for each( var snd:ManagedSound in this.sounds )
			{
				snd.resumeAfter( 0 );
			}
			
			Oboe.flags["SOUNDMUTED"] = false;
		}
		
		public function slot_onToggleMute():void
		{
			if( Oboe.flags.SOUNDMUTED )
			{
				slot_unmute();
			}
			else
			{
				slot_mute();
			}	
		}
		
		public function slot_mute():void
		{
			if( Oboe.flags["SOUNDMUTED"] ) return;
			
			if( this.music ) music.setMute( true );
			
			for each( var snd:ManagedSound in this.sounds )
			{
				snd.setMute( true );
			}
			
			Oboe.flags["SOUNDMUTED"] = true;
		}
		
		public function slot_unmute():void
		{
			if( !Oboe.flags["SOUNDMUTED"] ) return;

			if( this.music ) music.setMute( false );
			
			for each( var snd:ManagedSound in this.sounds )
			{
				snd.setMute( false );
			}
			
			Oboe.flags["SOUNDMUTED"] = false;
		}
	}
}

